import './index.less';

// 接口都写在src\scripts\services,每个页面一个文件
import LoginService from "../../../services/loginService.js";
const loginService = new LoginService();

export default class list {
    constructor() {
        this.url = ''
        this.timer = null;
        this.render();
    }
    //渲染页面
    async render() {
        let _this = this;
        //调用接口服务，获取后端的数据
        var state = this.GetQueryString("state");
        _this.url= this.GetQueryString("from")
        $(".demo").text("正在登录中...")
        _this.checkLogin({ 'state': state })
        this.timer = setInterval(() => {
            _this.checkLogin({ 'state': state })
        }, 1000);


 
        //处理逻辑
        //....开发区域

    }
    async checkLogin(options) {
        var _this = this;
        let result = await loginService.getLiveList(options);

        if (result.code == 200) {
            // $(".demo").text(JSON.stringify(result))
            console.log(result)
            console.log("转化前",_this.url,result.data.result == 'success')
            if (result.data.result == 'success') {
                console.log("转化后");
                console.log(result.data.token)
                console.log(typeof result.data.token)
                var url = window.atob(_this.url);
                if(url.indexOf('?')!= -1){
                    url = url + '&token='+ result.data.token + '&t=' + Date.parse(new  Date());
                }else{
                    url = url + '?token='+ result.data.token + '&t=' + Date.parse(new  Date());
                }
                console.log(url);
                clearInterval(_this.timer)
                window.location.href = url;
            }

        }
    }

    GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
        var context = "";
        if (r != null)
            context = r[2];
        reg = null;
        r = null;
        return context == null || context == "" || context == "undefined" ? "" : context;
    }
}